package com.lemon.secondhandplatform.ui.activity.good;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lemon.secondhandplatform.BaseApplication;
import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.Good;
import com.lemon.secondhandplatform.service.ShpService;
import com.lemon.secondhandplatform.ui.activity.BaseActivity;
import com.lemon.secondhandplatform.ui.activity.comment.CommentActivity;
import com.lemon.secondhandplatform.ui.activity.order.OrderActivity;
import com.lemon.secondhandplatform.ui.activity.user.UserActivity;
import com.lemon.secondhandplatform.ui.adapter.GoodAdapter;
import com.lemon.secondhandplatform.ui.util.StatusBarUtils;
import com.lemon.secondhandplatform.ui.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.datatype.BmobRelation;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.UpdateListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class GoodActivity extends BaseActivity {

    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    @BindView(R.id.navigationView)
    NavigationView navigationView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    CircleImageView imgAvator;

    private GoodAdapter adapter;
    private List<Good> goods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good);
        bindService();

        initView();
        bindListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUI();
    }

    private void updateUI() {
        if (binder != null) {
            binder.listGoods(goods == null ? 0 : goods.size(), new FindListener<Good>() {
                @Override
                public void done(List<Good> list, BmobException e) {
                    if (e == null) {
                        adapter.append(list);
                    }
                }
            });
        }

        Glide.with(this)
                .load(BaseApplication.getUser().getAvator())
                .into(imgAvator);
    }

    private void initView() {
        StatusBarUtils.drawableStatus(this, R.color.colorPrimary);
        View headerView = navigationView.getHeaderView(0);
        TextView tvNickname = headerView.findViewById(R.id.tvNickname);
        tvNickname.setText(BaseApplication.getUser().getNickname());
        imgAvator = headerView.findViewById(R.id.imgAvator);

        toolbar.inflateMenu(R.menu.add);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new GoodAdapter(this, goods);
        recyclerView.setAdapter(adapter);
    }

    private void bindListener() {
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.itemAdd:
                        Intent intent = new Intent(GoodActivity.this, Add.class);
                        startActivity(intent);
                        break;
                }
                return false;
            }
        });

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.itemPerson:
                        Intent intent = new Intent(GoodActivity.this, UserActivity.class);
                        startActivity(intent);
                        break;

                    case R.id.itemOrder:
                        Intent intent2 = new Intent(GoodActivity.this, OrderActivity.class);
                        startActivity(intent2);
                        break;

                    case R.id.itemMyGoods:
                        Intent intent3 = new Intent(GoodActivity.this, MyGoodsActivity.class);
                        startActivity(intent3);
                        break;
                }
                drawerLayout.closeDrawer(Gravity.LEFT);

                return false;
            }
        });

        adapter.setOnGoodItemClickListener(new GoodAdapter.OnGoodItemClickListener() {
            @Override
            public void onLikeClick(final View v, final Good good) {
                final ImageButton imgBtn = (ImageButton) v.findViewById(R.id.imgBtnLike);

                if (good.isLike()) {
                    binder.cancelLike(good, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            imgBtn.setImageResource(R.drawable.ic_heart_gray);
                            good.setLike(false);
                            TextView tvLikesCounter = (TextView) v.findViewById(R.id.tvLikesCounter);
                            tvLikesCounter.setText(good.getLikeCounter().toString());
                        }
                    });
                } else {
                    binder.like(good, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            imgBtn.setImageResource(R.drawable.ic_heart_red);
                            good.setLike(true);
                            TextView tvLikesCounter = (TextView) v.findViewById(R.id.tvLikesCounter);
                            tvLikesCounter.setText(good.getLikeCounter().toString());
                        }
                    });
                }
            }

            @Override
            public void onCommentsClick(View v, Good good) {
                Intent intent = CommentActivity.newIntent(GoodActivity.this, good);
                startActivity(intent);
            }

            @Override
            public void onMoreClick(View v, Good good) {

            }

            @Override
            public void onReserveClick(View v, final Good good) {
                binder.reserve(good, new QueryListListener<BatchResult>() {
                    @Override
                    public void done(List<BatchResult> list, BmobException e) {
                        if (e == null) {
                            goods.remove(good);
                            adapter.refresh(goods);
                            ToastUtils.show("预定成功");
                        } else {
                            ToastUtils.show("网络不给力");
                        }
                    }
                });
            }
        });
    }


    @Override
    protected void afterBindService(ShpService.ShpBinder binder) {
        super.afterBindService(binder);
        binder.listGoods(goods == null ? 0 : goods.size(), new FindListener<Good>() {
            @Override
            public void done(List<Good> list, BmobException e) {
                if (e == null) {
                    goods = list;
                    adapter.refresh(goods);
                }
            }
        });
    }

}
